package com.aqarat.managedBean;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.aqarat.controller.AqaratController;
import com.aqarat.controller.AqaratControllerImpl;
import com.aqarat.entity.Apartment;
import com.aqarat.entity.Building;
import com.aqarat.entity.Customer;
import com.aqarat.entity.Floor;
import com.aqarat.entity.Payment;
import com.aqarat.entity.resultsetmapping.PaymentResultSet;
import com.core.managedBean.BaseBean;

/**
 * @author karimsherif
 * 
 */
@ManagedBean
@ViewScoped
public class PaymentsBean extends BaseBean {
	private static final long serialVersionUID = 1L;

	private AqaratController aqaratController;
	private PaymentResultSet paymentResultSet;
	private PaymentResultSet selectedPaymentResultSet;
	// private List<Lookup> paymentStatusList;
	private List<PaymentResultSet> paymentResultSetList;
	private Customer customer;

	@PostConstruct
	public void loadData() {
		if (aqaratController == null) {
			aqaratController = new AqaratControllerImpl();
		}
		loadCustomerAndPayments();
		// loadAllLists();
	}

	// public void loadAllLists(){
	// try {
	// paymentStatusList=aqaratController.getLookupList(LookupType.PAYMENT_STATUS.getRecid());
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	public void loadCustomerAndPayments() {

		Object customerObject = super.getFlashAttribute("customer");
		customer = new Customer();
		paymentResultSet = new PaymentResultSet();
		if (customerObject != null) {
			customer = (Customer) customerObject;
			paymentResultSet.setCustomerId(customer.getRecid());
			loadPaymentResultSetList();
		}
	}

	public void searchPayments() {
		loadPaymentResultSetList();
	}

	public void loadPaymentResultSetList() {
		paymentResultSetList = aqaratController.getPaymentResultSetList(paymentResultSet);
	}

	public List<Customer> searchCustomer(String customerName) {
		try {
			return aqaratController.getCustomerListByName(customerName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public void payApayment() {
//		if (!paymentResultSet.getCustomerId().equals(selectedPaymentResultSet.getCustomerId())) {
//			super.addErrorMessage("this customer not that you select");
//			return;
//		}
		Payment payment = new Payment();
		
		payment.setRecid(selectedPaymentResultSet.getRecid());
		payment.setMoney(selectedPaymentResultSet.getMoney());
		payment.setIsFirstPayment(selectedPaymentResultSet.getIsFirstPayment());
		payment.setCustomerId(selectedPaymentResultSet.getCustomerId());
		payment.setPaymentDate(selectedPaymentResultSet.getPaymentDate());

		payment.setApartmentId(selectedPaymentResultSet.getApartmentId());
		payment.setApartment(new Apartment());
		payment.getApartment().setFloor(new Floor(selectedPaymentResultSet.getFloorId()));
		payment.getApartment().getFloor().setBuilding(new Building(selectedPaymentResultSet.getBuildingId()));
		try {
			aqaratController.payApayment(payment);
			loadPaymentResultSetList();
		} catch (Exception e) {
			super.addErrorMessage("An Error Occured");
			e.printStackTrace();
		}
	}

	public void prepareToPay() {
		System.out.println("paymentResultSet"+paymentResultSet);
	}

	// =====================================================================//
	public final PaymentResultSet getPaymentResultSet() {
		return paymentResultSet;
	}

	public final void setPaymentResultSet(PaymentResultSet paymentResultSet) {
		this.paymentResultSet = paymentResultSet;
	}

	public final List<PaymentResultSet> getPaymentResultSetList() {
		return paymentResultSetList;
	}

	public final void setPaymentResultSetList(List<PaymentResultSet> paymentResultSetList) {
		this.paymentResultSetList = paymentResultSetList;
	}

	public PaymentResultSet getSelectedPaymentResultSet() {
		return selectedPaymentResultSet;
	}

	public void setSelectedPaymentResultSet(PaymentResultSet selectedPaymentResultSet) {
		this.selectedPaymentResultSet = selectedPaymentResultSet;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public Customer getCustomer() {
		return customer;
	}

}
